package org.gensokyo.crawler.util

import java.io.File

import org.apache.commons.io.FileUtils
import org.gensokyo.crawler.EnumSerializer
import org.slf4j.LoggerFactory
import us.codecraft.webmagic.pipeline.Pipeline
import us.codecraft.webmagic.{ResultItems, Task}

import scala.collection.mutable.ListBuffer

/**
  * Created by liusen on 16-11-30.
  */
class SingalPageJsonFilePipeline(filePath: String = "") extends Pipeline {

  import org.json4s._
  import org.json4s.jackson.JsonMethods._

  import scalaj.collection.Imports._

  implicit val formats = org.json4s.DefaultFormats

  private val logger = LoggerFactory.getLogger(this.getClass)

  override def process(resultItems: ResultItems, task: Task): Unit = {
//    FileUtils.forceMkdir(new File(filePath).getParentFile)

    val res = resultItems.getAll.asScalaMutable.values.toList.headOption.getOrElse(List.empty)
    res match {
      case l: List[AnyRef] => {
        if (l.nonEmpty) {
          SingalPageJsonFilePipeline.content ++= l
          def writeToFile = synchronized {
            scala.tools.nsc.io.File(filePath).writeAll(pretty(render(Extraction.decompose(SingalPageJsonFilePipeline.content))))
          }
          writeToFile
        }
      }
      case _ => logger.error(s"res type : ${res.getClass.getName}")
    }
  }
}

object SingalPageJsonFilePipeline {
  val content = ListBuffer.empty[AnyRef]

  def test = {
    new SingalPageJsonFilePipeline("7.txt").process(new ResultItems().put("asdf", List(1, 2, 3)), null)
  }

  def main(args: Array[String]): Unit = {
    test
  }
}